package core

import (
	. "filter/file"
	"fmt"
	"sync"
)

const MaxChanSize = 10

// var f.Encoding string

// var chanfiles [MaxChanSize]File
// type Files struct {
// }

type NameFile struct {
	FileName string
	Files    []File
}

type CryptoFile struct {
	FileCrypto StringCodings
	Files      []File
}

type Filters struct {
	ROOT           string
	Encoding       string
	Lock           sync.Mutex
	NameFiles      map[string]NameFile
	CryptoFiles    map[string]CryptoFile
	DuplicateFiles []File
}

func (f *Filters) SetEncoding(s string) {
	fmt.Println("SetEncoding", s)
	f.Encoding = s
}

func (f *Filters) Init() {
	f.NameFiles = make(map[string]NameFile)
	f.CryptoFiles = make(map[string]CryptoFile)
	if f.Encoding == "" {
		f.Encoding = "CRC32"
	}
}

func (f *Filters) ProcessFolder(folder Folder) {
	fi := make([]File, 0)
	GetFile(folder.Path, &fi)
	// fmt.Println("正在处理", fi)
	for _, v := range fi {
		// var tmpFile File
		// fmt.Println("处理:", v.Name)
		tmpNF, flagNF := f.NameFiles[v.Name]
		tmpCF, flagCF := f.CryptoFiles[string(v.Code[f.Encoding])]
		var nf NameFile
		var cf CryptoFile

		if flagNF == false {
			nf.FileName = v.Name
			nf.Files = make([]File, 0)
			nf.Files = append(nf.Files, v)

		} else {
			tmpNF.Files = append(tmpNF.Files, v)
			nf = tmpNF

		}
		if flagCF == false {
			cf.FileCrypto = v.Code[f.Encoding]
			cf.Files = make([]File, 0)
			cf.Files = append(cf.Files, v)

		} else {
			tmpCF.Files = append(tmpCF.Files, v)
			cf = tmpCF
			f.DuplicateFiles = append(f.DuplicateFiles, v)
		}

		f.NameFiles[v.Name] = nf
		f.CryptoFiles[string(v.Code[f.Encoding])] = cf
	}
}
